﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

using System.Web;
using System.Data;
using System.IO;
using System.Xml;
using System.Linq;

namespace Deeper.Common
{
	public static class StringHelper
	{

		/// <summary>
		/// 使用指定字符集将String转换为Bytes
		/// </summary>
		/// <param name="text"></param>
		/// <param name="encoding">编码集</param>
		/// <returns></returns>
		public static byte[] ToBytes(this string text, Encoding encoding)
		{
			return encoding.GetBytes(text);
		}

		/// <summary>
		/// 使用指定字符集将byte[]转换成string
		/// </summary>
		/// <param name="encoding">编码集</param>
		public static string ToString(this byte[] bytes, Encoding encoding)
		{
			return encoding.GetString(bytes);
		}

		/// <summary>
		/// 替换回车换行符为html换行符
		/// </summary>
		public static string StrFormatHtmlBR(this string str)
		{
			return str.IsNull() ? string.Empty : str.Replace("\r\n", "<br />").Replace("\n", "<br />");
		}

		#region 删除最后一个字符之后的字符

		/// <summary>
		/// 删除最后结尾的一个逗号
		/// </summary>
		public static string DelLastComma(this string str)
		{
			return DelLastChar(str, ",");
		}

		/// <summary>
		/// 删除最后结尾的指定字符后的字符
		/// </summary>
		public static string DelLastChar(this string str, string strchar)
		{
			return (str.IsNull() || str.LastIndexOf(strchar) < 0) ? str : str.Substring(0, str.LastIndexOf(strchar));
		}

		#endregion

		#region 字符串转其他类型



		/// <summary>
		/// 字符串转Double
		/// </summary>
		/// <param name="str"></param>
		/// <param name="defaultValue">缺省值</param>
		/// <returns></returns>
		public static double ToDouble(this string str, double defaultValue = 0.00)
		{
			double result;
			return double.TryParse(str, out result) ? result : defaultValue;
		}



		/// <summary>
		/// 转换为Decimal
		/// </summary>
		/// <param name="str"></param>
		/// <param name="defaultValue">缺省值</param>
		/// <returns></returns>
		public static decimal ToDecimal(this string str, decimal defaultValue = 0)
		{
			decimal result;
			return decimal.TryParse(str, out result) ? result : defaultValue;
		}


		/// <summary>
		/// 转换为long
		/// </summary>
		/// <param name="str"></param>
		/// <param name="defaultValue">缺省值</param>
		/// <returns></returns>
		public static long ToLong(this string str, long defaultValue = 0)
		{
			long result;
			return long.TryParse(str, out result) ? result : defaultValue;
		}

		/// <summary>
		/// 字符串转Int
		/// </summary>
		/// <param name="str"></param>
		/// <param name="defaultValue">缺省值</param>
		/// <returns></returns>
		public static int ToInt(this string str, int defaultValue = 0)
		{
			int result;
			return int.TryParse(str, out result) ? result : defaultValue;
		}




		/// <summary>
		/// 字符串转Bool
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static bool ToBoolean(this string str)
		{
			bool result;
			bool.TryParse(str, out result);
			return result;
		}


		/// <summary>
		/// 字符串转日期可为空
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static DateTime? ToDateTime(this string str)
		{
			if (str.IsNull()) return null;
			DateTime date;
			return DateTime.TryParse(str, out date) ? new DateTime?(date) : null;
		}

		/// <summary>
		/// 字符串转日期型
		/// </summary>
		/// <param name="str"></param>
		/// <param name="defaultValue">缺省值</param>
		/// <returns></returns>
		public static DateTime ToDate(this string str, DateTime defaultValue)
		{
			DateTime date;
			return DateTime.TryParse(str, out date) ? date : defaultValue;
		}

		#endregion


		#region 字符串截取

		/// <summary>
		/// 是否为汉字
		/// </summary>
		/// <param name="chr"></param>
		/// <returns></returns>
		private static bool IsChinese(this char chr)
		{
			return (int)chr >= 0x4E00 && (int)chr <= 0x9FA5;
		}


		/// <summary>
		/// 获取字符串的字节长度
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static int GetByteLength(this string str)
		{
			return System.Text.Encoding.Default.GetByteCount(str);
		}



		/// <summary>
		/// 中英文混合字符串截取指定长度,startidx从0开始 
		/// </summary>
		/// <param name="str"></param>
		/// <param name="startidx"></param>
		/// <param name="len"></param>
		/// <returns></returns>
		public static string SubStringByByte(this string str, int len, int startidx = 0)
		{
			if (str.IsNull()) return string.Empty;
			int Lengthb = str.GetByteLength();
			if (startidx + 1 > Lengthb) return "";
			if (Lengthb <= len) return str;
			int strOffset = 0;
			int lenTemp = 0;
			int strw = 0;//字符的宽度
			bool b = false;
			string rstr = "";
			for (int i = 0; i < str.Length; i++)
			{
				char c = str[i];
				if (strOffset >= startidx)
				{
					rstr = rstr + c;
					b = true;
				}
				if (c.IsChinese()) strw = 2; else strw = 1;
				strOffset = strOffset + strw;
				if (b)
				{
					lenTemp = lenTemp + strw;
					if ((lenTemp + 1) >= len) break;
				}
			}
			return rstr + "...";
		}


		#endregion

		#region 获取汉字首拼
		/// <summary>
		/// 获取拼音首字母
		/// </summary>
		/// <param name="CnStr"></param>
		/// <returns></returns>
		public static string GetPyCode(this string CnStr)
		{
			string strTemp = "";
			int iLen = CnStr.Length;
			int i = 0;

			for (i = 0; i <= iLen - 1; i++)
			{
				strTemp += GetFullPy(CnStr.Substring(i, 1));
			}

			return strTemp;
		}


		/// <summary>
		/// 获取拼音
		/// </summary>
		/// <param name="c"></param>
		/// <returns></returns>
		private static string GetFullPy(string cn)
		{

			byte[] arrCN = Encoding.Default.GetBytes(cn);
			if (arrCN.Length > 1)
			{
				int area = (short)arrCN[0];
				int pos = (short)arrCN[1];
				int code = (area << 8) + pos;
				int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
				for (int i = 0; i < 26; i++)
				{
					int max = 55290;
					if (i != 25) max = areacode[i + 1];
					if (areacode[i] <= code && code < max)
					{
						return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
					}
				}
				return cn;
			}
			else return cn;


		}
		#endregion


		/// <summary>
		/// 判断是否是整数
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool IsInteger(this string value)
		{
			if (string.IsNullOrEmpty(value)) return false;
			if (value[0] != '-' && !char.IsDigit(value[0])) return false;
			int i;
			return int.TryParse(value, out i);
		}

		
		/// <summary>
		/// XML编码
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		internal static string XmlEncode(this string value)
		{
			if (!string.IsNullOrEmpty(value))
			{
				using (StringWriter stringWriter = new StringWriter())
				{
					using (XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter))
					{
						xmlWriter.WriteString(value);
						xmlWriter.Flush();
						value = stringWriter.ToString();
					}
				}
			}
			return value;
		}



		/// <summary>
		/// JS脚本编码
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static string JsEncode(this string value)
		{
			if (!string.IsNullOrEmpty(value))
			{
				value = value.Replace("\\", "\\\\");
				value = value.Replace("\"", "\\\"");
				value = value.Replace("\'", "\\'");
				value = value.Replace("\r", "\\r");
				value = value.Replace("\n", "\\n");
			}
			return value;
		}

		/// <summary>
		/// 压缩文本，去掉回车换行tab之类的
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static string CompressText(this string value)
		{
			if (!string.IsNullOrEmpty(value))
			{
				value = Regex.Replace(value, @"[ \t]*\r?\n[ \t]*", "");
			}
			return value;
		}
		/// <summary>
		/// 删除HTML代码
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static string RemoveHtmlCode(this string value)
		{
			if (string.IsNullOrEmpty(value)) return string.Empty;

			value = Regex.Replace(value, "<script[^>]*>.*?</script>|<style[^>]*>.*?</style>|<!--.*?-->", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
			return Regex.Replace(value, @"</?\s*[\w:\-]+(\s*[\w:\-]+\s*(=\s*""[^""]*""|=\s*'[^']*'|=\s*[^\s=>]*))*\s*/?>", "").Trim();
		}



	

	}
}
